#include "MTFunctionDefinition.hpp"
#include "MTExpression.hpp"
#include "MTVariableDefinition.hpp"
#include "MTVariableDeclaration.hpp"
#include "MTIdentifier.hpp"
#include "MTStatement.hpp"
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>

namespace MathematicaTranslator
{
 	std::string* MTFunctionDefinition::generateCode(CodeGenContext& context)
	{

		//wrap statements in block to create new variable context
		MTBlock* functionBlock = new MTBlock();
		functionBlock->statements.push_back(&block);
		
		context.pushBlock(functionBlock);

		std::ofstream *genFileStream = context.getStream();

		*genFileStream << "double ";
		*genFileStream << *id.generateCode(context);
		*genFileStream << "(";

		for (int i = argList.size() - 1; i>=0; --i){
			*genFileStream << "double ";
			*genFileStream << *argList[i]->generateCode(context);
			if (i>0) *genFileStream << ", ";
		}

		*genFileStream << ") {\n"; 

		*genFileStream << "   return ";
		*genFileStream << *block.generateCode(context);
		*genFileStream << ";\n}\n\n";
		
		context.popBlock();

		#ifdef DEBUG
			std::cout<<"Function definition created" <<std::endl;
		#endif

		return new std::string("");

	}

}
